package softtech.apps.callblocker.adapter;

import java.util.ArrayList;

import softtech.apps.utility.FileUtility;
import softtech.apps.utility.MyDialog;
import softtech.apps.callblocker.R;
import softtech.apps.callblocker.model.ContentSmsBlock;
import softtech.apps.callblocker.model.DatabaseHanler;
import softtech.apps.callblocker.model.HeaderPhone;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class AdapterSmsSettingContent extends BaseAdapter{

	
	MyDialog myDialog;
	
	/**
	 * List content block 
	 */
	DatabaseHanler db ;
	ArrayList<ContentSmsBlock> mListContent;
	Context context;
	public AdapterSmsSettingContent(ArrayList<ContentSmsBlock> item, Context context){
		this.mListContent = item;
		this.context = context;
	}
	int count =30;
	@Override
	public int getCount() {
		
		if(mListContent.size()>=count){
		return count;
		}
		if(mListContent.size()<=0){
			return 1;
		}
		return mListContent.size();
	}

	@Override
	public Object getItem(int arg0) {
		
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		
		return 0;
	}

	@Override
	public View getView(int position, View vi, ViewGroup arg2) {
		
		
		if (mListContent == null || mListContent.equals(null)
				|| mListContent.size() == 0) {
			LayoutInflater inflater = LayoutInflater.from(arg2.getContext());
			vi = inflater.inflate(R.layout.item_search_notfound, arg2 , false);
			return vi;
		}
		
		ViewHolder viewholder = new ViewHolder();
		if(vi==null || vi.getTag() == null){
			LayoutInflater inflater = LayoutInflater.from(arg2.getContext());
			vi = inflater.inflate(R.layout.page_setting_sms_item_advancedsetting, arg2 , false);
			
			initviewHolder(vi,viewholder);
			vi.setTag(viewholder);
			
		}else{
			viewholder  = (ViewHolder) vi.getTag();
		}
		
		viewholder.tvSmsContent.setText(""+mListContent.get(position).getContent());
		
		
		
		final int id = position;
		viewholder.btSmsContentDelete.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(context);
				DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						switch (which) {
						case DialogInterface.BUTTON_POSITIVE:
							// No button clicked

							break;
						case DialogInterface.BUTTON_NEGATIVE:
							// Yes button clicked
							String content = mListContent.get(id).getContent();
							db.deleteContentSmsBlock(ContentSmsBlock.CONTENT+" =?", 
									new String[]{content});
							mListContent.remove(id);
							update(mListContent);
							break;
						}
					}
				};

				builder.setMessage("Are you sure?")
						.setNegativeButton("Yes", dialogClickListener)
						.setPositiveButton("No", dialogClickListener).show();
				
			}
		});
		final ViewHolder view = viewholder;
		viewholder.tvSmsContent.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				OnClickListener leftClick = new OnClickListener() {

					@Override
					public void onClick(View v) {
						if (myDialog.getKeyCodeString().length() == 0) {
							return;
						}
						view.tvSmsContent.setText(""+myDialog
								.getKeyCodeString().trim());
					}
				};
				myDialog = new MyDialog(context, "Edit new content",
						"Edit new content", "Save", "Cancel", leftClick,
						null, view.tvSmsContent.getText().toString().trim());
				myDialog.showDialog();
			}
		});
		
		return vi;
	}
	/**
	 * ViewHolder
	 *
	 */
	static class ViewHolder{
		/**
		 * Delete sms content
		 */
		Button btSmsContentDelete;
		/**
		 * Content sms
		 */
		TextView tvSmsContent;
	}
	/**
	 * init viewholder
	 */
	private void initviewHolder(View vi,ViewHolder viewholder){
		if(db == null){
			db= new DatabaseHanler(context);
		}
		
		viewholder.btSmsContentDelete = (Button) vi.findViewById(R.id.btSmsContentDelete);
		viewholder.tvSmsContent = (TextView)vi.findViewById(R.id.tvsmsSettingContent);
	}
	/**
	 * update List sms content
	 * @param item
	 */
	public void update(ArrayList<ContentSmsBlock> item){
		mListContent = item;
		notifyDataSetChanged();
	}

}
